package com.miracle.java2110.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.miracle.java2110.common.PageVo;
import com.miracle.java2110.common.R;
import com.miracle.java2110.mapper.VacationMapper;
import com.miracle.java2110.pojo.Vacation;
import com.miracle.java2110.service.VacationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("vacationService")
public class VacationServiceImpl implements VacationService {

    @Autowired
    private VacationMapper vacationMapper;


    @Override
    public PageVo<Vacation> getPageData(Map<String, Object> params) {
        //mybatis分页插件
        PageHelper.startPage(Integer.parseInt(params.get("page")+""),Integer.parseInt(params.get("limit")+""));

        List<Vacation> vacations = vacationMapper.selectByParams(params);

        PageInfo<Vacation> vacationPageInfo = new PageInfo<>(vacations);

        // 把分页数据 封装到 自定义的 分页对象中，layui 要求的 JSON 格式是 PageVo
        PageVo<Vacation> vacationPageVo = new PageVo<>();

        vacationPageVo.setCode(0);
        vacationPageVo.setMsg("查询用户分页数据");
        vacationPageVo.setCount(vacationPageInfo.getTotal());
        vacationPageVo.setData(vacationPageInfo.getList());
        return vacationPageVo;
    }

    @Override
    public List<Vacation> findVacation() {
        return vacationMapper.selectByExample(null);
    }

    @Override
    public R updateById(Vacation vacation) {
        int result = vacationMapper.updateByPrimaryKeySelective(vacation);

        System.out.println(vacation);
        if (result == 1) {
            return R.ok(200, "修改成功");
        } else {
            return R.error(400,"修改失败");
        }
    }



    @Override
    public R deleteById(Integer id) {
        int result = vacationMapper.deleteByPrimaryKey(id);

        if (result == 1) {
            return R.ok(200,"删除成功");
        } else {
            return R.error(400,"删除失败");
        }
    }

    @Override
    public R addVacation(Vacation vacation) {

        int result = vacationMapper.insertSelective(vacation);

        if (result == 1) {
            return R.ok(200, "添加成功");
        } else {
            return R.error(400, "添加失败");
        }
    }


}
